package com.engine.topyoung.duss.deptaskreport;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.topyoung.duss.util.CalUtil;
import com.engine.topyoung.duss.util.TaskReportUtil;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-10
 * @Description: 校验是否是第一次登录
 * @ModifyLog:
 **/

public class WeaDepTaskReportWeb {

    /**
     *@Description: 获取部门任务表数据
     *@Param: [request, response]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2021/3/10
     */
    @GET
    @Path("/getDepTaskData")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDepTaskData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String month = Util.null2String(request.getParameter("month"));
            String departmentid = Util.null2String(request.getParameter("departmentid"));
            JSONObject datas = new JSONObject();
            RecordSet rs = new RecordSet();
            if("".equals(month)){
                month = DateUtil.getMonth();
            }
            if("".equals( departmentid )){
                User user = HrmUserVarify.getUser( request, response );
                departmentid = user.getUserDepartment()+"";
            }

            //应得分数duePint
            //应得总分：开始时间与结束时间为本年的所有预估分值总和。
            String sql = "select isnull(sum(cast(fz as DECIMAL(19,2))),0) duePint\n" +
                    "from uf_bmzrw where bm =? and left(jhjsrq,7)=? and left(jhksrq,7) =?";
            rs.executeQuery(sql,departmentid,month,month);
            rs.next();
            String duePint = Util.null2String(rs.getString("duePint"));
            datas.put("duePint",duePint);

            //已得分数havePoint
            //计划结束日期(jhjsrq)为本年内，任务状态（rwzt）为已完成（2），且是否超期（sfcq）为否（1）的实际的分值（sjdfz ）的总和。
            sql = "select isnull(sum(sjdf),0) havePoint from uf_bmzrw where bm = ? and left(jhjsrq,7)= ? and rwzt='2'";
            rs.executeQuery(sql,departmentid,month);
            rs.next();
            String havePoint = Util.null2String(rs.getString("havePoint"));
            datas.put("havePoint",havePoint);

            //超期得分cqPoint
            //计划结束日期(jhjsrq)为本年内，任务状态（rwzt）为已完成（2），且是否超期（sfcq）为是（0）的实际的分值（sjdf）的总和。
            sql = "select isnull(sum(sjdf),0) cqPoint from uf_bmzrw where bm = ? and left(jhjsrq,7)= ? and rwzt='3'";
            rs.executeQuery(sql,departmentid,month);
            rs.next();
            String cqPoint = Util.null2String(rs.getString("cqPoint"));
            datas.put("cqPoint",cqPoint);

            //完成率
            //已得分数
            String ydcount = havePoint;

            //已得分数：计划结束日期(jhjsrq)为本月内，任务状态（rwzt）为已完成（2），且是否超期（sfcq）为否（1）的实际的分值（sjdfz ）的总和。
            sql = "select isnull(sum(cast(fz as DECIMAL(19,2))),0) wcqfz from uf_bmzrw where bm = ? and rwzt = 2 and left(jhjsrq,7)= ?";
            rs.executeQuery(sql,departmentid,month);
            rs.next();
            String wcqfz = Util.null2String(rs.getString("wcqfz"));

            //超期完成
            sql = "select datediff(day,jhksrq,sjwcrq) days,isnull(cast(fz as DECIMAL(19,2)),0) as fz from uf_bmzrw " +
                    "where bm = ? and left(jhjsrq,7)= ? and sjwcrq is not null";
            rs.executeQuery(sql,departmentid,month);
            String cqwc = "0";
            while (rs.next()){
                String days = Util.null2String(rs.getString("days"));
                String fz = Util.null2String(rs.getString("fz"));
                cqwc = CalUtil.fAdd(cqwc,CalUtil.fMultiply(TaskReportUtil.getMultipleDays(days),fz));
            }

            //超期未完成
            String cqwwc = "0";
            sql = "select datediff(day,jhksrq,CONVERT(varchar(10),GETDATE(),120)) days,isnull(cast(fz as DECIMAL(19,2)),0) as fz from uf_bmzrw " +
                    "where bm = ? and left(jhjsrq,7)= ? and (sjwcrq is null or sjwcrq = '')";
            rs.executeQuery(sql,departmentid,month);
            while (rs.next()){
                String days = Util.null2String(rs.getString("days"));
                String fz = Util.null2String(rs.getString("fz"));
                cqwwc = CalUtil.fAdd(cqwwc,CalUtil.fMultiply(TaskReportUtil.getMultipleDays(days),fz));
            }
            //预期分数
            JSONArray yqfs = TaskReportUtil.getFzDetail(departmentid,month.substring( 0,4 ));

            //实际分数
            JSONArray sjfs = TaskReportUtil.getSjdfDetail(departmentid,month.substring( 0,4 ));

            String cqcount = CalUtil.fAdd(cqwc,cqwwc);
            String wxlv = "0";
            if(CalUtil.fCompare(wcqfz,"0")>0||CalUtil.fCompare(cqcount,"0")>0){
                wxlv = CalUtil.fMultiply(CalUtil.fDivide(ydcount,CalUtil.fAdd(cqcount,wcqfz),4),"100");
            }
            datas.put("wclv",wxlv);
            datas.put("yqfs",yqfs);
            datas.put("sjfs",sjfs);
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "error_message", "请求异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }

}
